﻿using Microsoft.AspNetCore.Mvc;
using WebAPITest.Filter;

namespace WebAPITest.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class HomeController : ControllerBase
    {

        [ResultFilter]
        [HttpGet]//请求方式
        public void TestFilter()
        {

        }
        //定义服务变量
        private readonly IDataService _dataService;
        private readonly IScopedService _scopedService;
        private readonly ITransientService _transientService;
        private readonly ISingletonService _singletonService;

        /// <summary>
        /// 构造注入
        /// </summary>
        /// <param name="dataService"></param>
        public HomeController(IDataService dataService, IScopedService scopedService, SingletonService singletonService = null, ITransientService transientService = null)
        {
            _dataService = dataService;
            _scopedService = scopedService;
            _singletonService = singletonService;
            _transientService = transientService;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dataService">[FromServices] 服务类的接口 变量名</param>
        [HttpGet]//请求方式
        public void Index([FromServices] IDataService dataService2, [FromServices] IScopedService scopedService2,[FromServices] ITransientService transientService2)
        {
            //1.需要自己去实例化类 一旦类发生改变 就会影响很多地方
            //DataService dataService = new DataService();
            //var data = dataService.GetData();

            //2.依赖注入 把new实例化 这一步交给IOC容器去完成
            _dataService.GetData();
            dataService2.GetHashCode();

            //这是从构造方法取得的
            var s=_scopedService.GetHashCode();
            //这是从方法取得的
            var s1 = scopedService2.GetHashCode();

            var t1=transientService2.GetHashCode();
            var t2=transientService2.GetHashCode();
        }

        [HttpGet]//请求方式
        public int SingletonFun()
        {
               return _singletonService.GetHashCode();
        }
        [HttpGet]//请求方式
        public string TransientFun([FromServices]ITransientService transientService2)
        {
            var t1 = _transientService.GetHashCode();
            var t2 = transientService2.GetHashCode();
            return $"t1={t1},t2={t2}";

        }
        [HttpGet]//请求方式
        public string ScopedFun([FromServices] IScopedService scopedService2)
        {
            var s1 = _scopedService.GetHashCode();
            var s2 = scopedService2.GetHashCode();
            return $"s1={s1},s2={s2}";
        }

        [HttpGet]//请求方式
        public string SendMessage(string message,int memberId)
        {
            return "请求成功";
        }
        [HttpGet]//请求方式
        public string SendComment(string messgae,int vodId)
        {
            return "请求成功";
        }
    }
}
